const path = require('path')
const { fileUploadError, unSupportedFileType, publishGoodsError, invalidGoodsId, findGoodsErr, changeGoodsTypeError } = require('../consitan/err.type')
const { createGoods, updateGoods, findGoods, changeType } = require('../service/goods.service')
const { goodsType } = require("../static/dictionaries/good.dic");
class GoodsController {
    async upload(ctx, next) {
        const { file } = ctx.request.files
        console.log(file)
        const fileTypes = ['image/jpeg', 'image/png']
        if(file) {
            if(!fileTypes.includes(file.mimetype)) {
                return ctx.app.emit('error', unSupportedFileType, ctx)
            }
            ctx.body = {
                code: 0,
                message: '商品上传成功',
                result: {
                    goodsImg: path.basename(file.filepath)
                }
            }
        }else {
            return ctx.app.emit('error', fileUploadError, ctx)
        }
    }
    async create(ctx, next) {
        try{
            const { createdAt, updatedAt, ...res } = await createGoods(ctx.request.body)
            ctx.body = {
                code: 0,
                message: '商品发布成功',
                result: res
,            }
        } catch (err) {
            console.error(err)
            return ctx.app.emit('error', publishGoodsError, err)
        }
    }
    async update(ctx, next) {
        try {
            const res = await updateGoods(ctx.params.id, ctx.request.body)
            console.log(res)
            if(res) {
                ctx.body = {
                    code: 0,
                    message: '商品修改成功',
                    result: ''
                }
            } else {
                return ctx.app.emit('error', invalidGoodsId, ctx)
            }
        } catch (err) {
            console.error(err)
        }
    }
    async change(ctx, next) {
        try {
            const res = await changeType(ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 0,
                    message: '商品状态更改成功',
                    result: ''
                }
            }
            console.log('商品状态更改成功')
        } catch (err) {
            console.log(err)
            return ctx.app.emit('error', changeGoodsTypeError, err)
        }
    }
    async list(ctx, next) {
        try {
            const req = ctx.request.query
            const table = {
                pageSize: +req.pageSize,
                pageNum: +req.pageNum
            }
            const { data: res, total } = await findGoods(req.goodsName || null, table)
            if(res) {
                ctx.body = {
                    code: 0,
                    message: '商品列表查询成功',
                    result: {
                        total: total,
                        rows: res.map(item => {
                            const { createdAt, updatedAt, ...ele } = item.dataValues
                            return ele
                        }),
                    }
                }
                console.log('列表页查询成功')
            }
        } catch (err) {
            return ctx.app.emit('error', findGoodsErr, ctx)
        }
    }
    async getGoodsType(ctx, next) {
        ctx.body = {
            code: 0,
            message: '商品类型获取成功',
            result: goodsType || []
        }
        console.log('商品类型获取成功')
    }
}

module.exports = new GoodsController()